package cminus;



import java.awt.*;
import java.io.*;
//import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
//import java.util.Map;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.awt.Frame;
import java.io.FileInputStream;
import java.io.IOException;
import cminus.Compiler;
import cminus.ast.ASTNode;
import cminus.ast.ASTPrinter;
import cminus.graph.Test;
import cminus.graph.layer;
//import cminus.ast.ASTNode;
//import cminus.ast.ASTPrinter;
//import cminus.ic.ICPrinter;
//import cminus.ic.Location;
import javax.swing.*;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Insets;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.FocusListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyVetoException;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.KeyStroke;
import javax.swing.tree.TreePath;

@SuppressWarnings("serial")
public class EditorFrame extends Frame implements ActionListener {
	 TextArea textArea2 = new TextArea();
	
	private JScrollPane treePane;  //存放树的可滚动容器
	private JSplitPane mainSplitPane;	//整个界面的分隔组建的容器
	private JTree tree;   //项目树
	private JSplitPane editorSplitPane;	//用于分隔主编辑区和信息显示区的容器
	private JScrollPane infoPane;	//可以滚动的JScrollPane对象，用于放infoArea
	private JTextArea infoArea;   //用于显示信息的文本域
	private JScrollPane infoPane1;
	private AddFrame addFrame;
	JTextArea textArea = new JTextArea("", 5, 50);
	
//	private TreeCreator treeCreator;
   
  // TextArea textArea2 = new TextArea("",5,50);
 
	public int newsTime = 0;//用来判断是刚开始打开 创建   还是已经创建过了

	/**第五步添加的组件**/
	private TreeCreator treeCreator;

   /*创建文件保存类  */
   MenuBar menuBar = new MenuBar();
   Menu fileMenu = new Menu("File");
   MenuItem newItem = new MenuItem("New");
   
   MenuItem newproject = new MenuItem("New Project");
   MenuItem newfolder = new MenuItem("New Folder");
   MenuItem newfile= new MenuItem("New File");
   
   MenuItem openItem = new MenuItem("Open");
   MenuItem refreshItem = new MenuItem("Refresh");
   MenuItem runItem= new MenuItem("Run");
   MenuItem saveItem = new MenuItem("Save");
   MenuItem saveAsItem = new MenuItem("Save As");
   MenuItem exitItem = new MenuItem("Exit");
   
   /*创建文件编辑 模块  */
   Menu editMenu = new Menu("Edit");
   MenuItem selectItem = new MenuItem("Select All");
   MenuItem copyItem = new MenuItem("Copy");
   MenuItem cutItem = new MenuItem("Cut");
   MenuItem pasteItem = new MenuItem("Paste");
   
   /*创建文件编译模块  */
   Menu HelpMenu = new Menu("help");
   MenuItem HelpItem = new MenuItem("help");
   MenuItem AboutItem = new MenuItem("about");
   
   Menu CompilerMenu = new Menu("complier");
   MenuItem ParserItem = new MenuItem("Parser");
   MenuItem lexerItem = new MenuItem("lexer");
   MenuItem SymbolTableItem = new MenuItem("SymbolTable"); 
   MenuItem ICGeneratorItem = new MenuItem("IC Generator"); 
   MenuItem CompilerItem = new MenuItem("Compiler");

   //对文件的操作
  public static String fileName = null;
   Toolkit toolKit=Toolkit.getDefaultToolkit();
   Clipboard clipBoard=toolKit.getSystemClipboard();
   
   private FileDialog openFileDialog= new FileDialog(this,"Open File",FileDialog.LOAD);
   private FileDialog saveAsFileDialog= new FileDialog(this,"Save File As",FileDialog.SAVE);

   private WorkSpace workSpace;
//编辑导航条
  @SuppressWarnings("static-access")
public void initFrame(WorkSpace space)
{
	this.workSpace = space;
    setTitle("Cminus Compiler");
    setFont(new Font("Times New Roman",Font.PLAIN,15));
    setBackground(Color.white);
    setSize(800,600);
    fileMenu.add(newItem);
    fileMenu.add(openItem);
    fileMenu.addSeparator();
    fileMenu.add(newproject);
    fileMenu.add(newfolder);
    fileMenu.addSeparator();
    fileMenu.add(newfile);
    fileMenu.add(saveItem);
    fileMenu.addSeparator();
    fileMenu.add(saveAsItem);
    fileMenu.add(refreshItem);
    fileMenu.addSeparator();
    fileMenu.add(runItem);
    fileMenu.add(exitItem);


    
    editMenu.add(selectItem);
    editMenu.addSeparator();
    editMenu.add(copyItem);
    editMenu.add(cutItem);
    editMenu.add(pasteItem);
   
    CompilerMenu.add(CompilerMenu);
    CompilerMenu.add(lexerItem );
    CompilerMenu.add(ParserItem);
    CompilerMenu.add(SymbolTableItem);
    CompilerMenu.add(ICGeneratorItem);
    
    HelpMenu.add(HelpItem);
    HelpMenu.add(AboutItem);
    
    menuBar.add(fileMenu);
    menuBar.add(editMenu);
    menuBar.add(CompilerMenu);
    menuBar.add(HelpMenu);
    setMenuBar(menuBar);
    
    //快捷按键栏
    Icon icon1 = new ImageIcon("images/1.jpg");
    Icon icon2 = new ImageIcon("images/2.jpg");
    Icon icon3 = new ImageIcon("images/3.jpg");
    Icon icon4 = new ImageIcon("images/4.jpg");
    
    Icon icon5 = new ImageIcon("images/5.jpg");
    Icon icon6 = new ImageIcon("images/6.jpg");
    Panel KeyWordPanel = new Panel();
    JButton LexerJBT  = new JButton(icon3);
    JButton ParserJBT  = new JButton(icon4);
    JButton ParserTreeJBT = new JButton(icon4);
    JButton SymbolTableJBT  = new JButton(icon5);
    JButton CodeGeneratorJBT  = new JButton(icon6);
    JButton CompilerJBT = new JButton(icon2);
    JButton OpenJBT  = new JButton(icon1);
    KeyWordPanel.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
    KeyWordPanel.add(OpenJBT);
    KeyWordPanel.add(CompilerJBT);
    KeyWordPanel.add(LexerJBT);
    KeyWordPanel.add(ParserJBT);
    KeyWordPanel.add(ParserTreeJBT);
    KeyWordPanel.add(SymbolTableJBT);
    KeyWordPanel.add(CodeGeneratorJBT);
   
    
    
  
    //快捷按键处理方法
    LexerJBT.addActionListener(new java.awt.event.ActionListener() {
		public void actionPerformed(java.awt.event.ActionEvent evt) {
			LexerOutPut();
		}
		});
    ParserJBT.addActionListener(new java.awt.event.ActionListener() {
		public void actionPerformed(java.awt.event.ActionEvent evt) {
			ParserOutPut();
		}
		});
    
    
    
    ParserTreeJBT.addActionListener(new java.awt.event.ActionListener() {
		public void actionPerformed(java.awt.event.ActionEvent evt) {
			Test t=new Test();
		}
		});
    
    
    
    SymbolTableJBT.addActionListener(new java.awt.event.ActionListener() {
		public void actionPerformed(java.awt.event.ActionEvent evt) {
			SymbolTableOutPut();
		}
		});
    CodeGeneratorJBT.addActionListener(new java.awt.event.ActionListener() {
		public void actionPerformed(java.awt.event.ActionEvent evt) {
			IcGeneratorOutPut();
		}
		});
    CompilerJBT.addActionListener(new java.awt.event.ActionListener() {
		public void actionPerformed(java.awt.event.ActionEvent evt) {
			DebugOutPut();
		}
		});
    OpenJBT.addActionListener(new java.awt.event.ActionListener() {
		public void actionPerformed(java.awt.event.ActionEvent evt) {
//			fileName = workSpace.getFolder().getPath()+getSelectionNode().getUserObjectPath()+getSelectionNode().getFileName() ;
			fileName = getSelectionNode().getFileName() ;
			if(fileName!= null)
		        System.out.print(fileName);
		    	 readFile(fileName);
		
		}
		});
 

	//创建信息显示区的文本域
    infoArea = new JTextArea("", 5, 50);
    infoPane = new JScrollPane(infoArea);
    infoArea.setEditable(false);
    infoPane1 = new JScrollPane(textArea);
    editorSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,infoPane1, infoPane);
	editorSplitPane.setDividerSize(3);
	editorSplitPane.setDividerLocation(200);
	add(editorSplitPane);
    add(KeyWordPanel,BorderLayout.NORTH);

    

    infoArea.setColumns(50);
    infoArea.setBackground(null);
    infoArea.setEditable(false);
    infoArea.setRows(10);



	treeCreator = new TreeCreator();
	tree = treeCreator.createTree(this);   //创建树的对象
	treePane  = new JScrollPane(tree); //创建可滚动的容器对象，承载项目树
	mainSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,treePane,editorSplitPane);	//创建主界面的JSplitPane,左面为treePane,右面为editorSplitPane
	mainSplitPane.setDividerLocation(200);
	mainSplitPane.setDividerSize(3);
	add(mainSplitPane);

	newproject.addActionListener(this);
    newfolder.addActionListener(this);
    newfile.addActionListener(this);
    newItem.addActionListener(this);
    openItem.addActionListener(this);
    saveItem.addActionListener(this);
    saveAsItem.addActionListener(this);
    refreshItem.addActionListener(this);
    runItem.addActionListener(this);
    exitItem.addActionListener(this);
    selectItem.addActionListener(this);
    copyItem.addActionListener(this);
    cutItem.addActionListener(this);
    ParserItem.addActionListener(this);
    lexerItem.addActionListener(this);
    ICGeneratorItem.addActionListener(this);
    SymbolTableItem.addActionListener(this);
    CompilerMenu.addActionListener(this);
    AboutItem.addActionListener(this);
    HelpItem.addActionListener(this);
   

    addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
            System.exit(0);
          }
        });
  }

  
  private void DefaultMutableTreeNode(String string) {
	// TODO Auto-generated method stub
	
}
  

/*形成响应事件 */
  @SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
    Object eventSource = e.getSource();
    if(eventSource == newItem)
    {
      textArea.setText("");
      if(newsTime ==1)
      {
    	  newsTime=0;  
      }
      if(fileName ==null)
      {
    	 newsTime = 1;  
      }
       fileName = null;
     }
    
   
    	
    	
    else if(eventSource == openItem)
    {
      openFileDialog.show();
      fileName = openFileDialog.getDirectory()+openFileDialog.getFile();
      if(fileName != null)
    	  System.out.print(fileName);
        readFile(fileName);
      
    }
    else if (eventSource == saveItem)
    {
      if(fileName != null)
        writeFile(fileName);
      else
      {
    	  saveAsFileDialog.show();
          fileName = saveAsFileDialog.getDirectory()+saveAsFileDialog.getFile();
          if (fileName!= null)
            writeFile(fileName);
      }
    }else if(eventSource == newproject)
    {
    	 newProject();
    	//	System.out.println("new project success");
    }else if(eventSource == newfolder)
    {
    	//System.out.println("new project folder");
    	 newFolder();
    }else if(eventSource == newfile )
    {
    	//System.out.println("new project newfile");
    	 newFile();
    }
    else if(eventSource == saveAsItem)
    {
      saveAsFileDialog.show();
      fileName = saveAsFileDialog.getDirectory()+saveAsFileDialog.getFile();
      if (fileName!= null)
        writeFile(fileName);
     }else if(eventSource == refreshItem)
     {
    	// System.out.println("refresh");
    	 reloadNode(getSelectionNode());
     }else if(eventSource == runItem)
     {
    	 System.out.println("run");
     }
    else if(eventSource == selectItem)
    {
       textArea.selectAll();
    }
    else if(eventSource == copyItem)
    {
       String text=textArea.getSelectedText();
       StringSelection selection=new StringSelection(text);
       clipBoard.setContents(selection,null);
    }
    else if(eventSource == cutItem)
    {
       String text=textArea.getSelectedText();
       StringSelection selection=new StringSelection(text);
       clipBoard.setContents(selection,null);
       textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd());
    }
    else if(eventSource == pasteItem)
    {
       Transferable contents=clipBoard.getContents(this);
       if(contents==null) return;
       String text;
       text="";
       try{
            text=(String)contents.getTransferData(DataFlavor.stringFlavor);
        }catch(Exception exception){
        }
       textArea.replaceRange(text,textArea.getSelectionStart(),textArea.getSelectionEnd());
    }
    

    //词法分析器 输出 模块 
    else if(eventSource == lexerItem){
    	this.LexerOutPut();
    	
    }
    
    //语法分析输出接口
    else if(eventSource == ParserItem){
    	this.ParserOutPut();
    }
    
    //符号表输出接口
    else if(eventSource == SymbolTableItem){
    	this.SymbolTableOutPut();
    }
    
    //代码生成输出接口
    else if(eventSource == ICGeneratorItem){
    	this.IcGeneratorOutPut();
    }
    
    else if(eventSource == CompilerMenu){
    	this.DebugOutPut();
    }
   
    //关于我们
    else if(eventSource == AboutItem){
    	this.about();
    }
    
    else if(eventSource == HelpItem){
    	this.Help();
      }
    //系统退出
    else if(eventSource == exitItem){
      System.exit(0);
    }
  }
  
//读文件
  public void readFile(String fileName){
    try{
      File file = new File(fileName);
      FileReader readIn = new FileReader(file);
      int size = (int)file.length();
      int charsRead = 0;
      char[] content = new char[size];
      while(readIn.ready())
        charsRead += readIn.read(content, charsRead, size - charsRead);
      readIn.close();
      textArea.setText(new String(content, 0, charsRead));
    }
    catch(IOException e){
      System.out.println("Error opening file");
    }
  }
//新建文件的方法
	public void newFile()
	{
		AddInfo info = new AddInfo("文件名称：", this,new AddFileHandler());
		showAddFrame(info);
	}
	//新建文件夹 
	public void newFolder()
	{
		AddInfo info = new AddInfo("目录名称：", this,new AddFolderHandler());
		showAddFrame(info);
	}
	//新建工程
	public void newProject()
	{
		AddInfo info = new AddInfo("项目名称：", this,new AddProjectHandler());
		showAddFrame(info);
	}
	
	/**
	 *获取当前选取的节点 
	 */
	public TreeNode getSelectionNode()
	{
		//获取当前树选择的节点在树中的路径
		TreePath path = tree.getSelectionPath();
		
		if(path!=null)
		{
			TreeNode selectNode =(TreeNode)path.getLastPathComponent();
			return selectNode;
		}
		return null;
	}
	
	
	//显示新增的界面
	private void showAddFrame(AddInfo info) {
		//使EditorFrame变成不可用
		setEnabled(false);
		addFrame = new AddFrame(info);
		addFrame.pack();
		addFrame.setVisible(true);
	}
	
	
	public JTree getTree() {
		// TODO Auto-generated method stub
		return tree;
	}
	public TreeCreator getTreeCreator() {
		return treeCreator;
	}

	public void setTreeCreator(TreeCreator treeCreator) {
		this.treeCreator = treeCreator;
	}
	//刷新树的节点
	public void reloadNode(TreeNode selectNode)
	{
		if(selectNode == null) return;
		//刷新树的节点
		Tree model = (Tree)getTree().getModel();
		model.reload(selectNode,treeCreator);
	}
	//刷新整棵树，即重新在treePane中构造一棵树
	public void refreshTree(JTree newTree)
	{
		//将tree对象变成参数中的newTree
		this.tree = newTree;
		//让treePane设置newTree为新的视图
		treePane.setViewportView(newTree);
		//更新界面
		treePane.updateUI();
	}
	
  //写文件
  public void writeFile(String fileName)
  {
    try{
      File file = new File (fileName);
      FileWriter writeOut = new FileWriter(file);
      writeOut.write(textArea.getText());
      writeOut.close();
    }
    catch(IOException e){
      System.out.println("Error writing file");
    }
  }
  
  
  //词法输出
  @SuppressWarnings("deprecation")
public void LexerOutPut() {
  try{
	  infoArea.setText(null);
		if (fileName == null)
		{
			if( newsTime !=1)
			{
				JOptionPane.showMessageDialog(null,"请先打开程序！");
				openFileDialog.show();
			      fileName = openFileDialog.getDirectory()+openFileDialog.getFile();
			      if(fileName != null)
			        readFile(fileName);
			      return;
			}else{
		      JOptionPane.showMessageDialog(null,"请先保存！");
		      saveAsFileDialog.show();
	          fileName = saveAsFileDialog.getDirectory()+saveAsFileDialog.getFile();
	          if (fileName!= null)
	            writeFile(fileName);
		      return;
			}
		    	
	    
		}
		FileInputStream inFile = new FileInputStream(fileName);
		Compiler compiler = new Compiler();
		compiler.setSource(inFile);
		compiler.compile();
		List<Token> TokenList = compiler.getTokenList();
		
		infoArea.append("Token:"+TokenList.size()+"\n"+"TokenList: \n");
		String OutPut1 =  new String();
		OutPut1 = " [line]   [column]   [token]         [paser]     [lexer]\n";
		infoArea.append(OutPut1);
		for(Token token:TokenList){
			String a=null;
			int b;
			String OutPut =  new String();
			OutPut =  "  "+token.getLine() + "         " + token.getColumn() + "               ";
			OutPut += token.getLexeme() + "                  ";
			infoArea.append(OutPut);
			if(token.getAttribute() == null) infoArea.append("null");
			else {
					b=token.getTokenType();
					switch(b){
				
					case 1: a="ID";break;
					case 2: a="INTEGER_LITERAL";break;
					case 3: a="KEYWORD";break;
					case 4: a="KEYWORD";break;
					case 5: a="KEYWORD";break;
					case 6: a="KEYWORD";break;
					case 7: a="KEYWORD ";break;
					case 8: a="KEYWORD";break;
					case 9: a="KEYWORD";break;
					case 10: a="L_BRACE";break;
					case 11: a="R_BRACE";break;
					case 12: a="L_PARENT";break;
					case 13: a="R_PARENT ";break;
					case 14: a="SEMI ";break;
					case 15: a="EQUAL";break;
					case 16: a="NOT ";break;
					case 17: a="PLUS ";break;
					case 18: a="MINUS";break;
					case 19: a="TIMES ";break;
					case 20: a="LESS ";break;
					case 21: a="LESS_EQUAL";break;
					case 22: a="GREATER ";break;
					case 23: a="GREATER_EQUAL ";break;
					case 24: a="EQUAL_EQUAL ";break;
					case 25: a="NOT_EQUAL";break;
					case 26: a="AND_AND ";break;
					case 27: a="OR_OR";break;
					case 28: a="DOT ";break;
					case 29: a="DIVIDE";break;
					case 30: a="KEYWORD";break;
					case 31: a="KEYWORD";break;
					case 32: a="KEYWORD";break;
					
					}
					if(a=="INTEGER_LITERAL"||a=="ID"){
						infoArea.append("  ");
						infoArea.append(token.getAttribute().toString());
					}
					else
						infoArea.append("      ");
					infoArea.append("          ");
					infoArea.append(a);
			
			}
			infoArea.append("\n");
		}
		
	 }catch(Exception exception){
		 exception.printStackTrace();
	}
  }
//语法输出
  @SuppressWarnings("deprecation")
public void ParserOutPut() {
  try{
	  infoArea.setText(null);
		if (fileName == null)
		{
			if( newsTime !=1)
			{
				JOptionPane.showMessageDialog(null,"请先打开程序！");
				openFileDialog.show();
			      fileName = openFileDialog.getDirectory()+openFileDialog.getFile();
			      if(fileName != null)
			        readFile(fileName);
			      return;
			}else{
		      JOptionPane.showMessageDialog(null,"请先保存！");
		      saveAsFileDialog.show();
	          fileName = saveAsFileDialog.getDirectory()+saveAsFileDialog.getFile();
	          if (fileName!= null)
	            writeFile(fileName);
		      return;
			}
		    	
	    
		}
		FileInputStream inFile = new FileInputStream(fileName);
		Compiler compiler = new Compiler();
		compiler.setSource(inFile);
		compiler.compile();
//		AbstractSyntaxTree ast = compiler.getAbstractSyntaxTree();
		ASTNode ast = compiler.getAbstractSyntaxTree();
	    layer l = new layer(); 
	    l.setLayer(ast);
	    l.setLayerNum(ast);
	    
	    Queue<ASTNode> q = new LinkedList<ASTNode>();
		q.add(ast);
		while (!q.isEmpty()){
			ASTNode n = q.remove();
			System.out.println(n.getLayerNum());
			for(int i=0;i<n.getChildrenNum();i++){
				
				 if(n.getChildIP(i)!=null){
					 		q.add(n.getChildIP(i));
						
				 }
			}
		}

		
		
		  ASTPrinter astPrinter = new ASTPrinter();
		  String OutPut =  new String();
		  OutPut = "Output AST: ";
		  infoArea.append(OutPut + "\n");
		  ((ASTNode) ast).accept(astPrinter);
		  infoArea.append(astPrinter.getOutPut());
		  
	 }catch(Exception exception){
		 exception.printStackTrace();
	}
  }

  //符号表输出
  @SuppressWarnings({ "unchecked", "deprecation" })
public void SymbolTableOutPut() {
 	// TODO Auto-generated method stub
	  try{
		  infoArea.setText(null);
			if (fileName == null)
			{
				if( newsTime !=1)
				{
					JOptionPane.showMessageDialog(null,"请先打开程序！");
					openFileDialog.show();
				      fileName = openFileDialog.getDirectory()+openFileDialog.getFile();
				      if(fileName != null)
				        readFile(fileName);
				      return;
				}else{
			      JOptionPane.showMessageDialog(null,"请先保存！");
			      saveAsFileDialog.show();
		          fileName = saveAsFileDialog.getDirectory()+saveAsFileDialog.getFile();
		          if (fileName!= null)
		            writeFile(fileName);
			      return;
				}
			    	
		    
			}
			FileInputStream inFile = new FileInputStream(fileName);
			Compiler compiler = new Compiler();
			compiler.setSource(inFile);
			compiler.compile();
//			AbstractSyntaxTree ast = compiler.getAbstractSyntaxTree();
//			ICGenerator icGenerator=new ICGenerator(ast);
//			icGenerator.generateIC();
//			Map<String, Location> symboltable = icGenerator.getHashMap();
			 
			String OutPut = new String();
			OutPut = "Output SymbolTable: ";
			infoArea.append(OutPut + "\n");
//			Iterator it = symboltable.keySet().iterator();
//			while(it.hasNext()) {
//				Object o = it.next();
//				textArea2.append(o +  "  level : 0" + "  dir  " + o.hashCode() + "  INT\n");
//			}
			  
		 }catch(Exception exception){
			 exception.printStackTrace();
		}

 }

  
  //代码生成输出方法
  @SuppressWarnings("deprecation")
public void IcGeneratorOutPut() {
		// TODO Auto-generated method stub
	  try{
		  infoArea.setText(null);
			if (fileName == null)
			{
				if( newsTime !=1)
				{
					JOptionPane.showMessageDialog(null,"请先打开程序！");
					openFileDialog.show();
				      fileName = openFileDialog.getDirectory()+openFileDialog.getFile();
				      if(fileName != null)
				        readFile(fileName);
				      return;
				}else{
			      JOptionPane.showMessageDialog(null,"请先保存！");
			      saveAsFileDialog.show();
		          fileName = saveAsFileDialog.getDirectory()+saveAsFileDialog.getFile();
		          if (fileName!= null)
		            writeFile(fileName);
			      return;
				}
			    	
		    
			}
			FileInputStream inFile = new FileInputStream(fileName);
			Compiler compiler = new Compiler();
			compiler.setSource(inFile);
			compiler.compile();
//			AbstractSyntaxTree ast = compiler.getAbstractSyntaxTree();
//			ICGenerator icGenerator=new ICGenerator(ast);
//			icGenerator.generateIC(); 
			
//			ICPrinter icPrinter = new ICPrinter();
			String OutPut =  new String();
			OutPut = "Output Intermediate Code: ";
			infoArea.append(OutPut + "\n");
//			icGenerator.getIC().accept(icPrinter);
//			textArea2.append(icPrinter.getICOutPut());
			  
		 }catch(Exception exception){
			 exception.printStackTrace();
		}
  }

  //调试信息输出  
@SuppressWarnings("deprecation")
public void DebugOutPut() {
	// TODO Auto-generated method stub
	try{
		infoArea.setText(null);
		if (fileName == null)
		{
			if( newsTime !=1)
			{
				JOptionPane.showMessageDialog(null,"请先打开程序！");
				openFileDialog.show();
			      fileName = openFileDialog.getDirectory()+openFileDialog.getFile();
			      if(fileName != null)
			        readFile(fileName);
			      return;
			}else{
		      JOptionPane.showMessageDialog(null,"请先保存！");
		      saveAsFileDialog.show();
	          fileName = saveAsFileDialog.getDirectory()+saveAsFileDialog.getFile();
	          if (fileName!= null)
	            writeFile(fileName);
		      return;
			}
		}
		FileInputStream inFile = new FileInputStream(fileName);
		Compiler compiler = new Compiler();
		compiler.setSource(inFile);
		compiler.compile();
		String lexMsg = compiler.LexMsg;
		String parseMsg = compiler.ParseMsg;
		
		if(lexMsg != "LexError: \n" || parseMsg != "ParseError: \n"){
			infoArea.append(lexMsg);
			infoArea.append(parseMsg);
		}
		else{
			infoArea.append("Done!");
		}
		
	 }catch(Exception exception){
		 exception.printStackTrace();
	}
}

  //关于信息
  public void about() {
		JOptionPane.showMessageDialog(this, "<-----这是一个用Java编写的C-语言编译器!----->\n"
				+ "<------by 朱明娟 刘岩 刘雪梅 祖常献------>\n", "C-语言编译器",
				JOptionPane.INFORMATION_MESSAGE);
	}

//帮助信息
	public void Help() {
		JOptionPane.showMessageDialog(this,
				"<--在输入代码域中输入C-语言代码，按Lexer、parser、code Generator、run 进行响应的操作和输出结果！-->\n"
						+ "<--保存文件\"File\"Save\"Open\"New进行源程序的保存、打开和创建!-->\n"
						+ "<--编辑文件\"Edit\"Select All\"Copy\"Cut\"paste进行源程序的全选 、复制 、剪切、删除等操作!-->\n","使用方法",JOptionPane.INFORMATION_MESSAGE);
	}


	 //快捷键响应事件
	
	@SuppressWarnings("deprecation")
public static void main(String[] args)
	{
		EditorFrame editorFrame = new EditorFrame();
		SpaceFrame spaceFrame =  new SpaceFrame(editorFrame);
		spaceFrame.setVisible(true);
	//	editorFrame.setVisible(true);
	}


	public WorkSpace getWorkSpace() {
		return workSpace;
	}
	
	
   }

